package com.boc.langxiang.ui.adapter.me;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.boc.langxiang.R;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.lzy.imagepicker.bean.ImageItem;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class OrdersApplyRefundRVAdapter extends RecyclerView.Adapter<OrdersApplyRefundRVAdapter.MyHolder> {


    private Context mCtx;
    private List<ImageItem> mList;
    private LayoutInflater mInflater;
    private boolean mIsLast;


    public OrdersApplyRefundRVAdapter(Context context, List<ImageItem> list) {
        this.mCtx = context;
        this.mList = list;
        mInflater = LayoutInflater.from(context);
    }

    private OnImageClickListener mOnImageClickListener;

    public interface OnImageClickListener {
        void onImageClick(View v, int position);

        void onAddClick(View v, int position);

        void onDeleteClick(View v, int position);
    }

    public OnImageClickListener getOnImageClickListener() {
        return mOnImageClickListener;
    }

    public void setOnImageClickListener(OnImageClickListener onImageClickListener) {
        this.mOnImageClickListener = onImageClickListener;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = mInflater.inflate(R.layout.item_me_orders_apply, parent, false);
        return new MyHolder(inflate);
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, final int position) {
        if (position == mList.size()) {
            holder.ivMeOrdersApplydelete.setVisibility(View.GONE);
            holder.ivMeOrdersApplydelete.setVisibility(View.GONE);
            holder.ivMeOrdersApplyadd.setVisibility(View.VISIBLE);
            holder.ivMeOrdersApplyimg.setVisibility(View.GONE);
            holder.ivMeOrdersApplyadd.setImageResource(R.drawable.icon_me_apply_more);
        } else {
            holder.ivMeOrdersApplydelete.setVisibility(View.VISIBLE);
            holder.ivMeOrdersApplyimg.setVisibility(View.VISIBLE);
            holder.ivMeOrdersApplyadd.setVisibility(View.GONE);
            holder.ivMeOrdersApplyimg.setImageResource(R.drawable.image_transparent);
            PicassoImageLoader.getInstance().displyNoFileImage(mCtx,mList.get(position).path,holder.ivMeOrdersApplyimg);
        }

        if (mOnImageClickListener != null) {
            if (position != mList.size()) {
                holder.ivMeOrdersApplyimg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnImageClickListener.onImageClick(v, position);
                    }
                });
            } else {
                if (mList.size() <= 2) {
                    holder.ivMeOrdersApplyadd.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnImageClickListener.onAddClick(v, position);
                        }
                    });
                } else {
                    holder.ivMeOrdersApplyadd.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            holder.itemView.setVisibility(View.GONE);
                        }
                    });
                }
            }

            holder.ivMeOrdersApplydelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnImageClickListener.onDeleteClick(v, position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() + 1 > 3 ? 3 : mList.size() + 1;
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_me_orders_applyimg)
        ImageView ivMeOrdersApplyimg;
        @BindView(R.id.iv_me_orders_applyadd)
        ImageView ivMeOrdersApplyadd;
        @BindView(R.id.iv_me_orders_applydelete)
        ImageView ivMeOrdersApplydelete;
        @BindView(R.id.view_me_orders_bg)
        View viewMeOrdersBg;

        public MyHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
